package com.yiren.dbaa.module.practice_set.carmodel.presenter;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;

import com.yiren.backstage.currency.entity.CarModelEntity;
import com.yiren.backstage.currency.utils.StringUtil;
import com.yiren.backstage.db.dao.controller.GradeDbController;
import com.yiren.backstage.db.dao.controller.UserDbController;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.currency.dialog.CommonDialog;
import com.yiren.dbaa.module.practice_set.PracticeCarSetActivity;
import com.yiren.dbaa.module.practice_set.carmodel.contract.CarModelReleaseContract;
import com.yiren.dbaa.module.practice_set.carmodel.model.CarModelReleaseModel;
import com.yiren.dbaa.view.dialog.LoadDialog;
import com.yiren.dbaa.view.toast.MessageBox;

/**
 * @author lwb
 * @date : 2022/12/6 15:48
 * @description: 车模发布
 */
public class CarModelReleasePresenter implements CarModelReleaseContract.Presenter {

    private Activity activity;
    private CarModelReleaseContract.Model releaseModel;

    public CarModelReleasePresenter(Activity activity) {
        this.activity = activity;
        releaseModel = new CarModelReleaseModel(activity);
    }

    @Override
    public void releaseCarModel(CarModelEntity carModel, String userId, String updateDetails, int updateMode) {
        if (StringUtil.isEmpty(updateDetails)&& carModel.getCarModelVer()>0){
            MessageBox.show(activity,"请输入更新详情");
            return;
        }
        //首次发布设置范围为全部用户
        if (carModel.getCarModelVer() == 0){
            carModel.setUseRange(2);
        }
        String message;
        if (carModel.getCarModelVer() < carModel.getCarModelVerLast()){
            message = "确定将版本回退到V"+carModel.getCarModelVer()+"吗?";
        }else {
            message ="发布前请验证，避免用户更新导致无法正常使用， 确定现在发布?";
        }
        CommonDialog.showTwoBtn(activity,"操作确认",message
                ,"取消","发布",(dialog, which) -> {
                    if (which == -2) {
                    } else {
                        LoadDialog.show(activity,"正在发布数据!");
                        releaseModel.publishCarModel(carModel,userId,updateDetails,updateMode,new CommonCallBack() {
                            @Override
                            public void success(Object result) {
                                carModel.setCarModelVer(carModel.getCarModelVerLast()+1);
                                UserDbController.getInstance(false).update(carModel);
                                LoadDialog.dismiss(activity);
                                MessageBox.MessageAndVoiceModel(activity,"发布成功!");
                                activity.finish();
                                activity.startActivity(new Intent(activity, PracticeCarSetActivity.class));
                            }
                            @Override
                            public void fail(String error) {
                                LoadDialog.dismiss(activity);
                                activity.runOnUiThread(() ->  CommonDialog.showOneBtn(activity, "操作提示", "数据发布失败!", "我知道了", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                }));
                            }
                        });
                    }
                    dialog.dismiss();
                });


    }
}
